import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from 'cc';
import { Aircraft } from './Aircraft';
import { EVENT_NAMES } from '../../../utils/Enums';
import { JsonAsset } from 'cc';
import { GGResource } from '../../../framework/core/base/GGResource';
import { Material } from 'cc';
import { MeshRenderer } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AirportCtrl')
export class AirportCtrl extends Component {
    @property( {type: Node, displayName: "地板定位" } )
    floorNode: Node = null;
    
    @property( {type: Prefab, displayName: "砖块预制体" } )
    brickPre: Node = null;
    
    @property( {type: [Material], displayName: "砖块预制体" } )
    brickMaters: Material[] = [];

    // 机场的宽高
    private portWidthSize:number = 9
    private portHeightSize:number = 11
    
    // 关卡配置
    private targetJson:JsonAsset = null

    start() {
        this.initListener()
        // this.drawFloor()
        this.initValues()
    }

    protected onDestroy(): void {
        gg.systemEvent.off(EVENT_NAMES.AIRPORT_CLICKED, this.onAircraftClicked, this)
    }

    // ----------------- 初始化 ----------------- //
    private initListener() {
        gg.systemEvent.on(EVENT_NAMES.AIRPORT_CLICKED, this.onAircraftClicked, this)
    }

    private initValues() {
        const self = this
        GGResource.load("tables:json/关卡/关卡1", JsonAsset).then((ja: JsonAsset) => {
            self.targetJson = ja
            console.log ("读取了")
            self.drawFloor()
        });
    }

    // ----------------- 内部方法 ----------------- //
    // 添加地板
    private drawFloor() {
        this.floorNode.removeAllChildren()
        
        for (var ix = 0; ix < this.targetJson.json.length; ix++) {
            for (var iz = 0; iz < this.targetJson.json[ix].length; iz++) {
                const value = this.targetJson.json[ix][iz]

                const brickNode = instantiate( this.brickPre )
                brickNode.setPosition(new Vec3(ix - 0.5 * (this.portWidthSize - 1), 0, iz - 0.5 * (this.portHeightSize - 1)))
                brickNode.parent = this.floorNode
                brickNode.name = ix + "_" + iz
                
                if (value != 0) {
                    
                    const dir = Math.floor( value / 100 )
                    const grid = Math.floor((value % 100) / 10) - 1
        
                    const meshRenderer = brickNode.getComponent(MeshRenderer)
                    meshRenderer.material = this.brickMaters[grid]
    
                    if (dir == 0) {
                        brickNode.angle = 0
                        brickNode.eulerAngles = new Vec3(0, 0, 0)
                    } else if (dir == 1) {
                        brickNode.eulerAngles = new Vec3(0, 180, 0)
                    } else if (dir == 2) {
                        brickNode.eulerAngles = new Vec3(0, 90, 0)
                    } else if (dir == 3) {
                        brickNode.eulerAngles = new Vec3(0, 270, 0)
                    }
                }
            }
        }
    }
    
    // ----------------- 事件 ----------------- //
    private onAircraftClicked(aircraft: Aircraft) {
        console.log ("飞机飞")
    }
}


